package com.tanhua.server.controller;

import com.tanhua.model.Counts;
import com.tanhua.model.PageResult;
import com.tanhua.model.UserInfo;
import com.tanhua.model.vo.UserInfoVo;
import com.tanhua.model.vo.VisitorsVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.MovementService;
import com.tanhua.server.service.UserInfoService;
import com.tanhua.server.service.UserService;
import com.tanhua.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @author Administrator
 */
@RestController
@RequestMapping(path = "/users")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private MovementService movementService;

    /**
     * 查询用户基础信息
     *
     * @param userID
     * @param token
     * @return
     */
    @GetMapping
    public ResponseEntity userInfo(Long userID, @RequestHeader("Authorization") String token) {
        //1. 校验token
        boolean verifyToken = JwtUtils.verifyToken(token);
        if(!verifyToken) {
            return ResponseEntity.status(401).body(null);
        }
        //2. 获取token中的用户信息
        Claims claims = JwtUtils.getClaims(token);
        String id = claims.get("id").toString();

        if (userID == null) {
            userID = Long.valueOf(id);
        }

        UserInfoVo userInfo = userInfoService.findById(userID);

        return ResponseEntity.ok(userInfo);
    }

    //互相喜欢，喜欢，粉丝 - 统计
    @GetMapping("counts")
    public ResponseEntity counts(){
        Counts counts = userInfoService.findCounts();
        return ResponseEntity.ok(counts);
    }

    //修改手机号- 1 发送短信验证码
    @PostMapping("/phone/sendVerificationCode")
    public void sendVerificationCode(){
        //1. 获取手机号
        String phone = UserHolder.getMobile();
        //2. 发送验证码
        userService.sendCode(phone);
    }

    //修改手机号 - 2 校验验证码
    @PostMapping("/phone/checkVerificationCode")
    public boolean checkVerificationCode(@RequestBody Map map){
        String phone = UserHolder.getMobile();
        String code = (String) map.get("verificationCode");
        boolean b = userInfoService.checkVerificationCode(phone, code);
        return b;
    }

    //修改手机号 - 3 保存
    @PostMapping("/phone")
    public void SavePhone(@RequestBody Map<String, String> map){
        //1. 获取手机号
        String phone = map.get("phone");
        Long userId = UserHolder.getUserId();
        userInfoService.savePhone(userId,phone);
    }

    //更新用户头像
    @PostMapping("/header")
    public ResponseEntity header(MultipartFile headPhoto) throws IOException {
        Long userId = UserHolder.getUserId();
        userInfoService.updateHead(headPhoto, Long.valueOf(userId));
        return ResponseEntity.ok(null);
    }


    //互相喜欢、喜欢、粉丝、谁看过我 - 翻页列表
    @GetMapping("/friends/{type}")
    public ResponseEntity friendsType(@RequestParam(defaultValue = "1")  Integer page,
                                      @RequestParam(defaultValue = "10") Integer pagesize,
                                      @PathVariable("type") String type){
        //1 互相关注
        //2 我关注
        //3 粉丝
        //4 谁看过我
        switch (type){
            case "1":
                PageResult result1 = userInfoService.findfriend(page,pagesize);
                return ResponseEntity.ok(result1);
            case "2":
                PageResult result2 = userInfoService.findMyloves(page,pagesize);
                return ResponseEntity.ok(result2);
            case "3":
                PageResult result3 = userInfoService.findFans(page,pagesize);
                return ResponseEntity.ok(result3);
            case "4":
                List<VisitorsVo> visitorsVos = movementService.queryVisitorsList();
                return ResponseEntity.ok(visitorsVos);
        }
        return ResponseEntity.ok(null);
    }
    //喜欢粉丝
    @PostMapping("/fans/{uid}")
    public ResponseEntity fansLike(@PathVariable("uid") Long uid){
        Long userId = UserHolder.getUserId();
        userInfoService.fansLike(userId,uid);
        return ResponseEntity.ok(null);
    }

    //取消喜欢粉丝
    @DeleteMapping("/like/{uid}")
    public ResponseEntity fansUnLike(@PathVariable("uid")Long uid){
        Long userId = UserHolder.getUserId();
        userInfoService.fansUnlike(userId,uid);
        return ResponseEntity.ok(null);
    }
}
